package com.supercube.sport.yoga.controller;

import com.github.pagehelper.PageInfo;
import com.supercube.common.utils.ShiroUtils;
import com.supercube.common.utils.StringUtils;
import com.supercube.common.utils.TimeUtils;
import com.supercube.sport.yoga.bo.MemberBO;
import com.supercube.sport.yoga.domain.MemberBodyInfoDTO;
import com.supercube.sport.yoga.domain.MemberDTO;
import com.supercube.sport.yoga.domain.MemberIntegralRecordDTO;
import com.supercube.sport.yoga.service.MemberService;
import com.supercube.sport.yoga.utils.ExcelUtils;
import com.supercube.sport.yoga.utils.ResultInfo;
import com.supercube.sport.yoga.vo.BookingCourseBo;
import com.supercube.sport.yoga.vo.MemberAndCardVo;
import com.supercube.sport.yoga.vo.MemberConsumeRecordVo;
import com.supercube.sport.yoga.vo.MemberVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

@RestController
@RequestMapping("/member")
@Api(tags = "会员信息")
@Slf4j
public class MemberController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MemberService memberService;

    /**
     * 保存会员信息
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "保存会员信息", notes = "保存会员信息")
    @PostMapping("/save")
    public ResultInfo saveMember(@RequestBody @Validated MemberDTO dto) {
        // 校验手机号唯一
        String menberId = memberService.queryPhone(dto.getPhone());
        if (StringUtils.isNotBlank(menberId)) {
            return ResultInfo.failed("该手机号已存在");
        } else {
            String id = UUID.randomUUID().toString();
            dto.setId(id);
            Long userId = ShiroUtils.getUserId();
            dto.setCreateTime(TimeUtils.getCurrentTime());
            dto.setCreateUser(userId.toString());
            dto.setLastModifyUser(userId.toString());
            dto.setLastModifyTime(TimeUtils.getCurrentTime());
            dto.setStatus("1");
            memberService.save(dto);
            return ResultInfo.success("保存成功");
        }
    }

    /**
     * 修改会员信息
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "修改会员信息", notes = "修改会员信息")
    @PostMapping("/update")
    public ResultInfo updateMenber(@RequestBody MemberDTO dto) {
        dto.setLastModifyUser(ShiroUtils.getUserId().toString());
        dto.setLastModifyTime(TimeUtils.getCurrentTime());
        // 校验手机号唯一
        String id = memberService.queryPhone(dto.getPhone());
        if (StringUtils.isNotBlank(id) && !Objects.equals(id,dto.getId())) {
            return ResultInfo.failed("该手机号已存在");
        }
        memberService.update(dto);
        return ResultInfo.success("修改成功");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除会员信息", notes = "删除会员信息")
    @GetMapping("/delete")
    public ResultInfo deleteMember(@RequestParam("id") String id) {
        memberService.deleteMember(id);
        return ResultInfo.success("删除成功");
    }
    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除会员信息", notes = "删除会员信息")
    @GetMapping("/batchDeleteMember")
    public ResultInfo batchDeleteMember(@RequestParam("ids[]") List<String> ids) {
        if(ids== null || ids.size()==0){
            return ResultInfo.failed("缺少参数ids");
        }
        memberService.batchDeleteMember(ids);
        return ResultInfo.success("删除成功");
    }
    /**
     * 查询详情
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "查询详情", notes = "查询详情")
    @GetMapping("/memberInfo")
    public ResultInfo memberInfo(@RequestParam("id") String id) {
        MemberVO vo = memberService.memberInfo(id);
        return ResultInfo.success(vo);
    }

    /**
     * 查询会员列表
     *
     * @param map
     * @return
     */
    @ApiOperation(value = "查询会员列表", notes = "查询会员列表")
    @PostMapping("/list")
    public ResultInfo selectMemberList(@RequestBody MemberBO bo) {
        List<MemberVO> list = memberService.selectMemberList(bo);
        PageInfo<MemberVO> pageInfo = new PageInfo<>(list);
        return ResultInfo.success(pageInfo);
    }
    /**
     * 查询名称或手机号查询会员
     *
     * @param map
     * @return
     */
    @ApiOperation(value = "查询名称或手机号查询会员", notes = "查询名称或手机号查询会员，参数{'text':'test'} ")
    @PostMapping("/selectMember")
    public ResultInfo selectMember(@RequestBody Map<String,Object> map) {
        List<MemberVO> list = memberService.selectMember(map);
        return ResultInfo.success(list);
    }
    /**
     * 查询会员消费记录
     * @param memberId
     * @return
     */
    @ApiOperation(value = "查询会员消费记录", notes = "查询会员消费记录,memberId cardId")
    @PostMapping("/getConsumeRecord")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "memberId", value = "会员id", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "cardId", value = "会员卡id", dataType = "string", paramType = "query")}
    )
    public ResultInfo getConsumeRecord(@RequestBody Map<String,String> map) {
        String memberId = map.get("memberId");
        String cardId = map.get("cardId");
        if(StringUtils.isAllEmpty(memberId,cardId)){
            return ResultInfo.failed("会员id和会员卡id不能都为空");
        }
        List<MemberConsumeRecordVo> list = memberService.getConsumeRecord(memberId,cardId);
        return ResultInfo.success(list);
    }

    /**
     * 查询会员体测信息
     * @param memberId
     * @return
     */
    @ApiOperation(value = "查询会员体测信息", notes = "查询会员体测信息")
    @GetMapping("/getMemberBodyInfo")
    public ResultInfo getMemberBodyInfo(@RequestParam("memberId") String memberId) {
        List<MemberBodyInfoDTO> list = memberService.getMemberBodyInfo(memberId);
        return ResultInfo.success(list);
    }

    /**
     * 查询会员体测信息
     * @param memberId
     * @return
     */
    @ApiOperation(value = "查询会员体测信息-带文件", notes = "查询会员体测信息-带文件")
    @GetMapping("/queryMemberBodyInfoForFile")
    public ResultInfo queryMemberBodyInfoForFile(@RequestParam("memberId") String memberId, @RequestParam("bodyExaminationTime") String bodyExaminationTime) {
        List<MemberBodyInfoDTO> list = memberService.queryMemberBodyInfoForFile(memberId,bodyExaminationTime);
        return ResultInfo.success(list);
    }

    /**
     * 查询会员积分信息
     * @param memberId
     * @return
     */
    @ApiOperation(value = "查询会员积分信息", notes = "查询会员积分信息")
    @GetMapping("/getMemberIntegral")
    public ResultInfo getMemberIntegral(@RequestParam("memberId") String memberId) {
        List<MemberIntegralRecordDTO> list = memberService.getMemberIntegral(memberId);
        return ResultInfo.success(list);
    }
    /**
     * 查询会员预约课程
     * @param memberId
     * @return
     */
    @ApiOperation(value = "查询会员预约课程", notes = "查询会员预约课程")
    @GetMapping("/getBookingCourse")
    public ResultInfo getBookingCourse(@RequestParam("memberId") String memberId) {
        List<BookingCourseBo> list = memberService.getBookingCourse(memberId);
        return ResultInfo.success(list);
    }

    @ApiOperation(value = "导出会员列表", notes = "导出会员列表")
    @PostMapping("/exportMemberAndCard")
    public void exportMemberAndCard (HttpServletResponse response) {
        try {
            List<MemberAndCardVo> list = memberService.selectMemberAndCard();
            ExcelUtils.exportExcel(response,"会员表","静波瑜伽会员表",list,MemberAndCardVo.class);
        } catch (Exception e) {
            log.error("导出会员列表", e);
        }
    }
}
